<?php

namespace App\Library\Common;

use App\Kuafu\User\Enum\UserCodeEnum;

class CommonTools
{

    /**
     * @return bool
     */
    public static function isWebRequest(): bool
    {
        return self::getRequestFrom() == UserCodeEnum::WEB_TYPE;
    }

    /**
     * 获取请求来源
     *
     * @return int
     */
    public static function getRequestFrom(): int
    {
        $middlewareGroup = request()->route()->middleware();
        foreach ($middlewareGroup as $middleware) {
            $parsedMiddleware = explode(":", $middleware);
            if (isset($parsedMiddleware[0]) && $parsedMiddleware[0] == "jwtAuth") {
                if (
                    isset($parsedMiddleware[1])
                    &&
                    in_array($parsedMiddleware[1], array_keys(UserCodeEnum::PLATFORM_TYPE_TEXT))
                ) {
                    return $parsedMiddleware[1];
                }
            }
        }
        return 0;
    }
}
